package com.casa.POI.dianping;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Properties;
import java.util.List;

public class ConfigurationManager {
	
	private static final String CONSOLE_COLS = "consoleCols";
	private static final String REDIAL = "redial";
	private static final String CITY_LIST = "cityList";
	private static final String MAX_THREADS = "maxThreads";
	private static final String BATCH_INTERVAL_SECONDS = "batchIntervalSeconds";
	private static final String BATCH_SIZE = "batchSize";
	private static ConfigurationManager configurationManager = new ConfigurationManager();
	private Properties properties;
	
	public static ConfigurationManager getInstance() {
		return configurationManager;
	}
	
	protected ConfigurationManager() {
		this.properties = new Properties();
		InputStream is = getClass().getResourceAsStream("/spider.properties");
		try {
			this.properties.load(is);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public int getBatchSize() {
		String value = this.properties.getProperty(BATCH_SIZE, String.valueOf(200));
		return Integer.parseInt(value);
	}
	
	public int getBatchIntervalSeconds() {
		String value = this.properties.getProperty(BATCH_INTERVAL_SECONDS, String.valueOf(60));
		return Integer.parseInt(value);
	}
	
	public int getMaxThreads() {
		String value = this.properties.getProperty(MAX_THREADS, String.valueOf(10));
		return Integer.parseInt(value);
	}
	
	public int getConsoleCols() {
		String value = this.properties.getProperty(CONSOLE_COLS, String.valueOf(80));
		return Integer.parseInt(value);
	}
	
	public boolean getRedial() {
		String value = this.properties.getProperty(REDIAL, String.valueOf(false));
		return Boolean.parseBoolean(value);
	}
	
	public Integer[] getCities() {
		List<Integer> cities = new ArrayList<Integer>();
		String value = this.properties.getProperty(CITY_LIST);
		for (String val : value.split(",")) {
			cities.add(Integer.parseInt(val));
		}
		return cities.toArray(new Integer[0]);
	}
}
